package com.item.controller;

import com.item.pojo.*;
import com.item.service.PictureService;
import com.item.service.ShopService;
import com.item.service.VehicleService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class VehicleController {

    @Resource
    private VehicleService vehicleService;

    @Resource
    private PictureService pictureService;

    @Resource
    private ShopService shopService;

    private final String PATH = "D:\\S4\\item\\nginx-1.21.6\\html\\medical-html\\html\\page\\images\\";


    @PostMapping("/addVehicles")
    public boolean addStore(Vehicle vehicle, HttpSession session) {
        System.out.println("vehicle:" + vehicle);
        Picture picture = (Picture) session.getAttribute("picture");
        vehicle.setVehicle_image(picture == null ? 0 : picture.getPictureId());
        if (picture == null) {
            Picture data = new Picture("", 1);
            pictureService.addPicture(data);
            vehicle.setVehicle_image(data.getPictureId());
        } else {
            vehicle.setVehicle_image(picture.getPictureId());
        }
        if (vehicleService.saveOrUpdate(vehicle)) {
            session.setAttribute("picture", null);
            return true;
        }
        return false;
    }


    @RequestMapping("/findVehicle")
    public Map<String, Object> findVehicle(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                           @RequestParam(value = "limit", required = false, defaultValue = "3") Integer limit,
                                           @RequestParam(value = "vehicleType", required = false) String vehicleType,
                                           @RequestParam(value = "vehicleState", required = false) Integer vehicleState,
                                           @RequestParam(value = "store", required = false) Integer store) {
        return vehicleService.getQueryPage(vehicleType, vehicleState, store, page, limit);
    }


    @PostMapping("/vehicleDelete")
    public boolean delete(Integer vid, Integer pictureId, String image) {
        if (vehicleService.removeById(vid) && pictureService.removeById(pictureId)) {
            File file = new File(PATH + image);
            file.delete();
            return true;
        }
        return false;
    }


    @PostMapping("/updateState")
    public boolean state(Integer vid, Integer state) {
        System.out.println("vid:" + vid + "\t\tstate:" + state);
        return vehicleService.saveOrUpdate(new Vehicle(vid, state));
    }

    @PostMapping("/updateVehicles")
    public Map<String, Object> updateVehicles(Vehicle vehicle) {
        System.out.println("vehicle:" + vehicle);
        Map<String, Object> map = new HashMap<>();
        if (vehicleService.saveOrUpdate(vehicle)) {
            map.put("result", "修改成功");
        } else {
            map.put("result", "修改失败");
        }
        return map;
    }


    @PostMapping("/seatsList")
    public List<Seats> seats() {
        List<Seats> seatsList = new ArrayList<>();
        seatsList.add(new Seats(1, "2座"));
        seatsList.add(new Seats(2, "4座"));
        seatsList.add(new Seats(3, "6座"));
        seatsList.add(new Seats(4, "8座"));
        return seatsList;
    }

    @PostMapping("/boxTypesList")
    public List<BoxTypes> boxTypes() {
        List<BoxTypes> boxTypesList = new ArrayList<>();
        boxTypesList.add(new BoxTypes(1, "两厢"));
        boxTypesList.add(new BoxTypes(2, "三厢"));
        boxTypesList.add(new BoxTypes(3, "SUV"));
        boxTypesList.add(new BoxTypes(4, "商务车"));
        boxTypesList.add(new BoxTypes(5, "客车"));
        boxTypesList.add(new BoxTypes(6, "跑车"));
        boxTypesList.add(new BoxTypes(7, "旅行车"));
        boxTypesList.add(new BoxTypes(8, "房车"));
        boxTypesList.add(new BoxTypes(9, "轿车"));
        boxTypesList.add(new BoxTypes(10, "小型跑车"));
        return boxTypesList;
    }

    @PostMapping("/stateInfo")
    public List<State> stateInfo() {
        List<State> stateList = new ArrayList<>();
        stateList.add(new State(1, "租赁中"));
        stateList.add(new State(2, "已下架"));
        return stateList;
    }

    @PostMapping("/transmission_type")
    public List<State> transmission_type() {
        List<State> stateList = new ArrayList<>();
        stateList.add(new State(1, "自动"));
        stateList.add(new State(2, "手动"));
        return stateList;
    }


    @PostMapping("/displacements")
    public List<Displacements> displacements() {
        List<Displacements> displacementsList = new ArrayList<>();
        displacementsList.add(new Displacements(1, "1.5"));
        displacementsList.add(new Displacements(2, "2.0"));
        displacementsList.add(new Displacements(3, "2.3"));
        displacementsList.add(new Displacements(4, "5.0"));
        displacementsList.add(new Displacements(5, "V8"));
        displacementsList.add(new Displacements(6, "V12"));
        displacementsList.add(new Displacements(7, "1.0"));
        displacementsList.add(new Displacements(8, "4.0"));
        return displacementsList;
    }
}
